<?php
/**
 * P3数据插件
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: App.php 1059 2011-11-18 09:26:15Z mmxie $
 */
class IG_Data_Plugin_App extends IG_Data_Plugin
{
    public $view;
    protected $_width;

    public function setWidth($width)
    {
        $this->_width = $width;
        return $this;
    }

    public function __construct($config)
    {
        parent::__construct($config);
        $this->view = $this->getView();
    }

    public function before()
    {
        /*
        $limit = $this->getRequest()->getParam('limit');
        if ($limit) {
            $this->getSource()->setParams('limit', $limit);
        }*/
    }

    public function after()
    {
        $this->getView()->getHelper('call')->setPlugin($this);
        echo $this->call($this->getSource()->getBody());
    }

    public function afterXmlHttpRequest()
    {
        $body = $this->getSource()->getBody();
        echo $this->getView()->json(array(
            'sEcho' => $this->getRequest()->getParam('sEcho'),
            'iTotalRecords' => $body['body']['total'],
            'iTotalDisplayRecords' => $body['body']['total'],
            'aaData' => $body['body']['table']['rows']
        ));
    }

    public function call($data)
    {
        if (isset($data['__call'])) {
            $call = $data['__call'];
            unset($data['__call']);
            return $this->$call($data);
        }
        return print_r($data, true);
    }

    public function response($data)
    {
        if (!empty($data['error']['code'])) {
            return "<h1>错误({$data['error']['code']}):{$data['error']['message']}</h1>";
        }

        return $this->call($data['body']);
    }

    public function itemInfo($data, $id = null)
    {
//        return print_r($data, true);
        $this->view->itemData = $data;
        $this->view->id       = $id;
        return $this->view->render('p3/item-info.phtml');
    }

    public function image($data, $zoom = 1)
    {
        $p = $data['basicPostion'];
        $x = $zoom * $p['x'];
        $y = $zoom * $p['y'];
        $w = $zoom * $p['w'];
        $h = $zoom * $p['h'];
        $s = $zoom * 512;
        return "<img src=\"/images/none.gif\" "
             . " style=\"background:url(/images/source/{$data['iconName']}.jpg) "
             . "-{$x}px "
             . "-{$y}px no-repeat;"
             . "background-size: {$s}px {$s}px;\""
             . " width=\"$w\" "
             . " height=\"$h\" />";
    }

    public function loadResult($data)
    {
        return $this->table($data['table'], $data['total']);
    }

    public function table($data, $total = null)
    {
        $columns = $addSorting = array();
        foreach ($data['columns'] as $key => $column) {
            $columns[] = array(
                'sTitle'    => $column[0],
                'bSortable' => $column[1] != 0,
            );
            if (strtolower($column[2]) == 'none')
                $addSorting[] = array($key, $column[2]);
        }

        $options = array();
        if (count($data['rows']) != $total) {
            $options = array(
                'iDeferLoading' => $total,
                'bProcessing'   => true,
                'bServerSide'   => true,
                'bFilter'       => false,
                'aaSorting'     => $addSorting
            );
        }

        if ($this->_width) {
            $options += array(
                'sScrollX' => "100%",
                'sScrollXInner' => $this->_width,
                'bScrollCollapse' => true,
            );
        }

        return $this->view->dataTable(
            'gtable',
            $columns,
            $data['rows'],
            $options
        );
    }

    public function node($data)
    {
        $this->view->data = $data;
        return $this->view->render('p3/role.phtml');
    }

    public function itemAttr($data, $text4 = false)
    {
        return ($text4 ? $this->view->text($data[0]) : $data[0])
               . ": " . ($data[1] > 0 ? '+' : '-') . $data[1];
    }
}